package com.ruoyi.lock.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lock.domain.AFaultReport;
import com.ruoyi.lock.service.IAFaultReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 故障上报Controller
 * 
 * @author ruoyi
 * @date 2024-04-16
 */
@RestController
@RequestMapping("/falut/report")
public class AFaultReportController extends BaseController
{
    @Autowired
    private IAFaultReportService aFaultReportService;

    /**
     * 查询故障上报列表
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('falut:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(AFaultReport aFaultReport)
    {
//        Long userId = SecurityUtils.getUserId();
//        if (!SecurityUtils.isAdmin(userId)) {
//            aFaultReport.setUserId(userId);
//        }
        startPage();
        List<AFaultReport> list = aFaultReportService.selectAFaultReportList(aFaultReport);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('falut:report:list')")
    @GetMapping("/list1")
    public TableDataInfo list1(AFaultReport aFaultReport)
    {
        startPage();
        List<AFaultReport> list = aFaultReportService.selectAFaultReportList1(aFaultReport);
        return getDataTable(list);
    }

    @GetMapping("/listCount")
    public AjaxResult listCount(AFaultReport aFaultReport)
    {
        Long userId = SecurityUtils.getUserId();
        if (!SecurityUtils.isAdmin(userId)) {
            aFaultReport.setUserId(userId);
        }
        return success(aFaultReportService.listCount(aFaultReport));
    }

    /**
     * 导出故障上报列表
     */
    @PreAuthorize("@ss.hasPermi('falut:report:export')")
    @Log(title = "故障上报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AFaultReport aFaultReport)
    {
        List<AFaultReport> list = aFaultReportService.selectAFaultReportList(aFaultReport);
        ExcelUtil<AFaultReport> util = new ExcelUtil<AFaultReport>(AFaultReport.class);
        util.exportExcel(response, list, "故障上报数据");
    }

    /**
     * 获取故障上报详细信息
     */
    @PreAuthorize("@ss.hasPermi('falut:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aFaultReportService.selectAFaultReportById(id));
    }

    /**
     * 新增故障上报
     */
//    @PreAuthorize("@ss.hasPermi('falut:report:add')")
    @Log(title = "故障上报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AFaultReport aFaultReport)
    {
        return toAjax(aFaultReportService.insertAFaultReport(aFaultReport));
    }

    /**
     * 修改故障上报
     */
    @PreAuthorize("@ss.hasPermi('falut:report:edit')")
    @Log(title = "故障上报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AFaultReport aFaultReport)
    {
        return toAjax(aFaultReportService.updateAFaultReport(aFaultReport));
    }

    /**
     * 删除故障上报
     */
    @PreAuthorize("@ss.hasPermi('falut:report:remove')")
    @Log(title = "故障上报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aFaultReportService.deleteAFaultReportByIds(ids));
    }
}
